<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>

      <el-table-column prop="companyName" label="名称" width="180" sortable>
      </el-table-column>
      <el-table-column prop="companyNum" label="电话" width="120" sortable>
      </el-table-column>
      <el-table-column prop="registerTime" label="入驻时间" width="180" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="150" sortable>
        <template scope="scope">
          <span v-if="scope.row.state=='0'" style="color: blue">待审核</span>
          <span v-if="scope.row.state=='1'" style="color: orange">审核通过，待激活</span>
          <span v-if="scope.row.state=='2'" style="color: green">激活成功</span>
          <span v-if="scope.row.state=='3'" style="color: red">驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="商店地址" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="logo" label="商标" min-width="180" sortable>
      </el-table-column>
      <el-table-column prop="admin.username" label="店长" min-width="80" sortable>
      </el-table-column>
			<el-table-column label="操作" width="250">
				<template scope="scope">
<!--          <el-button type="warning" size="small" @click="handleCheck(scope.$index, scope.row)">审核</el-button>-->
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-button type="warning" @click="activate" :disabled="this.sels.length===0">邮件激活</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增或编辑界面
		:visible.sync="outerVisible" 新版本打开对话框
		v-model="tenantFormVisible" 老版本打开对话框
		-->
		<el-dialog title="新增或修改" :visible.sync="tenantFormVisible" :close-on-click-modal="false">
			<el-form :model="tenant" label-width="80px" :rules="tenantFormRules" ref="tenantForm">
				<el-form-item label="名称" prop="username">
					<el-input v-model="tenant.username" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="tenant.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="tenant.age" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="tenant.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="头像" prop="headImage">
          <el-input v-model="tenant.headImage" auto-complete="off"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="tenantFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="save" :loading="tenantLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--审核
        :visible.sync="outerVisible" 新版本打开对话框
        v-model="tenantFormVisible" 老版本打开对话框
        -->
    <el-dialog title="审核" :visible.sync="tenantFormVisible" :close-on-click-modal="false">
      <el-form :model="tenant" label-width="80px" :rules="tenantFormRules" ref="tenantForm">
        <el-form-item label="店铺名" prop="name">
          <el-input v-model="tenant.name" disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="座机号码" prop="tel">
          <el-input v-model="tenant.tel" disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="注册时间" prop="registerTime">
          <el-input v-model="tenant.registerTime" disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state">
            <el-input v-if="tenant.state==1" disabled="disabled" style="color: blue" value="待审核"></el-input>
            <el-input v-if="tenant.state==2" disabled="disabled" style="color: orange" value="审核通过，待激活"></el-input>
            <el-input v-if="tenant.state==3" disabled="disabled" style="color: green" value="激活成功"></el-input>
            <el-input v-if="tenant.state==4" disabled="disabled" style="color: red" value="驳回"></el-input>
        </el-form-item>
        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="tenant.address" disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺商标" prop="logo">
          <el-input v-model="tenant.logo" disabled="disabled" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" @click.native="reject">驳回</el-button>
        <el-button type="success" @click.native="adopt" :loading="tenantLoading">通过</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				//分页相关
				query:{
					currentPage:1,
					pageSize:10,
					keyword:''
				},
				pageInfo:{
					total:0,
					rows:[]
				},
				listLoading: false,
				sels: [],//列表选中列

				tenantFormVisible: false,//新增界面是否显示
				tenantLoading: false,
				tenantFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					]
				},
				//新增界面数据
        tenant: {
          id:null,
          key: '',
          value: '',
          intro: '',
        }
			}
		},
		methods: {
      //发送邮件激活店铺
      activate: function () {
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认发送邮件激活该店铺吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          if (ids.length==1){
            this.$http.get("/tenant/sendMessage/"+ids).then((res) => {
              this.listLoading = false;
              //NProgress.done();
              this.$message({
                message: '发送成功',
                type: 'success'
              });
              this.getTenants();
            });
          }else {
            this.listLoading = false;
            this.$message({
              message: '请指定一个店铺发送邮件！！',
              type: 'warning'
            });
          }
        }).catch(() => {
        });
      },
      //显示编辑界面
      handleCheck: function (index, row) {
        this.tenantFormVisible = true;
        this.tenant = Object.assign({}, row); //回显
      },
		  reject() {
        let para = {
          "id": this.tenant.id,
          "state": 4
        }
        this.$http.put("/tenant/check",para).then((res) => {
          res = res.data;
          if (res.success){
            this.$message({
              message: '驳回成功',
              type: 'success'
            });
            this.tenantFormVisible = false; //关闭对话框

            this.$refs['tenantForm'].resetFields(); //重置表单

            this.getTenants(); //重新加载数据
          }else{
            this.$message({
              message: '驳回失败,'+res.message,
              type: 'error'
            });
          }
          this.tenantLoading = false; //关闭加载条
        });
      },
      adopt() {
        let para = {
          "id": this.tenant.id,
          "state": 2
        };
        this.$http.put("/tenant/check",para).then((res) => {
          res = res.data;
          if (res.success){
            this.$message({
              message: '设置成功',
              type: 'success'
            });
            this.tenantFormVisible = false; //关闭对话框

            this.$refs['tenantForm'].resetFields(); //重置表单

            this.getTenants(); //重新加载数据
          }else{
            this.$message({
              message: '设置失败,'+res.message,
              type: 'error'
            });
          }
          this.tenantLoading = false; //关闭加载条
        });
      },
			search(){
				//重置当前页为第一页
				this.query.currentPage =1;
				this.getTenants();
			},
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			handleCurrentChange(val) {
				this.query.currentPage= val;
				this.getTenants();
			},
			//获取用户列表
      getTenants() {
        this.listLoading = true; //发起请求之前显示加载条
        //发起请求获取数据
        this.$http.post("/sysmanage/tenant/list",this.query)
            .then(result =>{
              result = result.data; //{success,result:{}}
              if (result.success){
                this.pageInfo = result.resultObj;
              }else{
                this.$message({
                  message: '获取分页数据失败！'+result.message,
                  type: 'error'
                });
              }
              this.listLoading = false;
            })
            .catch(result=>{
              this.$message({
                message: '系统错误！',
                type: 'error'
              });
            })
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = row.id;
          alert(row.id);
          this.$http.delete("/tenant/"+para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getTenants();
					});
				}).catch(() => {
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.tenantFormVisible = true;
				this.tenant = Object.assign({}, row); //回显
        console.log(this.tenant);
        this.getDepartments();
			},
			//显示新增界面
			handleAdd: function () {
				this.tenantFormVisible = true;
				this.tenant =  {
          id:null,
          name: '',
          password: '',
          age: '',
          email: '',
          headImage: '',
          department:{}
				}
        this.getDepartments();
			},
			//编辑
			save: function () {
				this.$refs.tenantForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.tenantLoading = true;
							let para = Object.assign({}, this.tenant);
              console.log(para);
							//para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							this.$http.put("/tenant",para).then((res) => {
								res = res.data;
								if (res.success){
									this.$message({
										message: '保存成功',
										type: 'success'
									});
									this.tenantFormVisible = false; //关闭对话框

									this.$refs['tenantForm'].resetFields(); //重置表单

									this.getTenants(); //重新加载数据
								}else{
									this.$message({
										message: '保存失败,'+res.message,
										type: 'error'
									});
								}
								this.tenantLoading = false; //关闭加载条
							});
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
          console.log(ids);
          this.$http.patch("/tenant/batch",ids).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getTenants();
					});
				}).catch(() => {
				});
			}
		},
		mounted() {
			this.getTenants();
		}
	}

</script>

<style scoped>

</style>
